package yjy.leeinn.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.Cell;
/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Yjygame implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	/**
	 * This is the entry point method.
	 */
	
	FlexTable maintab = new FlexTable();
	int cx,cy;
	Label cntstep=new Label();
	int cnt=0;
	public boolean checkwin(){
		//job before night jobs
		return true;
	}
	DialogBox dial = new DialogBox();
	VerticalPanel dialpanel = new VerticalPanel();
	public void onModuleLoad() {
		//init dialog
		dial.setText("congratulations!");
		dialpanel.add(new HTML("<span id='winmsg'>you win</span>"));
		dialpanel.setSize("300px", "200px");
		dial.setWidget(dialpanel);
		Button closebut = new Button("Close");
		dialpanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialpanel.setVerticalAlignment(VerticalPanel.ALIGN_BOTTOM);
		dialpanel.add(closebut);
		closebut.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				dial.hide();
			}
		});
		cntstep.setText("steps: "+cnt+"  ");
		RootPanel.get("cnt").add(cntstep);
		RootPanel root=RootPanel.get("gamediv");
		root.add(maintab);
		int i,j;
		int hi=400/3;
		int wi=400/3;
		int t=0;
		myran tool = new myran();
		for (i=0;i<3;i++){
			for (j=0;j<3;j++){
				Button but = new Button();
				but.setHeight(hi+"px");
				but.setWidth(wi+"px");
				but.addStyleName("comm");
				int val=tool.getint();
				if (val==0){
					but.addStyleName("space");
					cx=i;
					cy=j;
				}
				but.setText(val+"");
				maintab.setWidget(i, j,but);
				t++;
			}	
		}
		maintab.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				Cell c = maintab.getCellForEvent(event);
				if (c!=null){
					int x,y;
					x=c.getRowIndex();
					y=c.getCellIndex();
					String pos="("+x+","+y+")";
					if (x==cx&&y==cy){
						//Window.alert("center");
					}
					//Window.alert(pos);
					Button but=(Button)maintab.getWidget(x, y);
					//but.removeStyleName("gwt-Button");
					Button cen = (Button)maintab.getWidget(cx, cy);
					if (Math.abs(cx-x)+Math.abs(cy-y)==1){
						maintab.remove(but);
						maintab.remove(cen);
						maintab.setWidget(cx, cy, but);
						maintab.setWidget(x, y, cen);
						cx=x;	cy=y;
						cnt++;
						cntstep.setText("steps: "+cnt+"  ");
						if (checkwin()){
							dial.center();
						}
					}
				}
			}
		});
	}
}
